package vashion.azeroth.core.manager.item;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;

import vashion.azeroth.contant.AzerothConstants;
import vashion.azeroth.contant.BrandErrorEnum;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.common.NeedOperateFlow;
import vashion.azeroth.core.dao.BaseDao;
import vashion.azeroth.core.item.dao.ItemBrandDao;
import vashion.azeroth.core.item.pojo.BrandWithForestList;
import vashion.azeroth.core.item.pojo.ForestDO;
import vashion.azeroth.core.item.pojo.ItemBrandDO;
import vashion.azeroth.core.manager.impl.BaseManagerImpl;
import vashion.azeroth.core.pojo.common.OperateFlowEnum;
import vashion.azeroth.core.result.ApiResult;

/**
 * Created by Loki on 17/1/6. 基础的品牌管理
 */
@Service
public class BaseItemBrandManager extends BaseManagerImpl<ItemBrandDO> {

	private final static Logger log = LoggerFactory.getLogger(BaseItemBrandManager.class);

	@Autowired
	private ItemBrandDao itemBrandDao;
	@Autowired
	private BaseItemForestBrandManager baseItemForestBrandManager;

	@Override
	public BaseDao<ItemBrandDO> getDao() {
		// TODO Auto-generated method stub
		return itemBrandDao;
	}

	/**
	 * 查询品牌
	 *
	 * @param itemBrandDO
	 * @return
	 */
	public ApiResult<List<ItemBrandDO>> getItemBrandList(ItemBrandDO itemBrandDO) {
		ApiResult<List<ItemBrandDO>> result = new ApiResult<List<ItemBrandDO>>();
		try {
			itemBrandDO.setStatus(AzerothConstants.ItemBrand.NORMAL);
			List<ItemBrandDO> itemBrandList = (List<ItemBrandDO>) this.find(itemBrandDO);
			if (CollectionUtils.isEmpty(itemBrandList)) {
				return ApiResult.newSuccessResult();
			} else {
				result = (ApiResult<List<ItemBrandDO>>) ApiResult.newSuccessResult(itemBrandList);
				result.setTotalCount(itemBrandList.size());
			}
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return (ApiResult<List<ItemBrandDO>>) result;
	}

	/**
	 * 新增品牌
	 *
	 * @param brandDO
	 * @param operateId
	 * @param forestIds
	 * @return 新增品牌是否成功
	 */
	@NeedOperateFlow(operateIdArgLocation = 2, operateFlowEnum = OperateFlowEnum.BRAND)
	public ApiResult<Boolean> publishBrand(ItemBrandDO brandDO, String forestIds, long operateId) {
		if (null == brandDO || StringUtils.isBlank(brandDO.getTitle())) {
			return ApiResult.newErrorResult(BrandErrorEnum.ARG_ERROR.getErrorCode(),
					BrandErrorEnum.ARG_ERROR.getErrorMessage());
		}
		brandDO.setGmtCreate(new Date());
		brandDO.setGmtModified(new Date());
		brandDO.setUserId(operateId);
		// 是否已存在检查
		ItemBrandDO dbValue = this.findByTitle(brandDO.getTitle());
		if (null != dbValue) {
			return ApiResult.newErrorResult(BrandErrorEnum.ALREADY_EXITS.getErrorCode(),
					BrandErrorEnum.ALREADY_EXITS.getErrorMessage());
		}
		try {
			brandDO = this.save(brandDO);
			// brandDO = this.find(brandDO).get(0);
			// 保存类目品牌关联的表
			baseItemForestBrandManager.publishBrandForestIdList(brandDO, forestIds, operateId);
		} catch (Exception e) {
			log.error("BaseBrandManager.publishBrand error, forestD=" + brandDO, e);
			return ApiResult.newErrorResult(BrandErrorEnum.SYSTEM_ERROR.getErrorCode(),
					BrandErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return ApiResult.newSuccessResult(true);
	}

	/**
	 * 更新品牌
	 *
	 * @param brandDO
	 * @param operateId
	 * @param forestIds
	 * @return 更新品牌是否成功
	 */
	@NeedOperateFlow(operateIdArgLocation = 2, operateFlowEnum = OperateFlowEnum.BRAND)
	public ApiResult<Boolean> updateBrand(ItemBrandDO brandDO, String forestIds, long operateId) {
		if (null == brandDO || StringUtils.isBlank(brandDO.getTitle())) {
			return ApiResult.newErrorResult(BrandErrorEnum.ARG_ERROR.getErrorCode(),
					BrandErrorEnum.ARG_ERROR.getErrorMessage());
		}
		brandDO.setGmtModified(new Date());
		brandDO.setUserId(operateId);
		// 是否已存在检查
		ItemBrandDO dbValue = this.findByTitle(brandDO.getTitle());
		if (null != dbValue && dbValue.getId() != brandDO.getId()) {
			return ApiResult.newErrorResult(BrandErrorEnum.ALREADY_EXITS.getErrorCode(),
					BrandErrorEnum.ALREADY_EXITS.getErrorMessage());
		}
		try {
			if (1 <= this.update(brandDO)) {
				// 保存类目品牌关联的表
				baseItemForestBrandManager.updateBrandForestIdList(brandDO, forestIds, operateId);
			}
		} catch (Exception e) {
			log.error("BaseBrandManager.publishBrand error, forestD=" + brandDO, e);
			return ApiResult.newErrorResult(BrandErrorEnum.SYSTEM_ERROR.getErrorCode(),
					BrandErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return ApiResult.newSuccessResult(true);
	}

	/**
	 * @param brandDO
	 * @param needForests
	 * @return 带有相关类目的品牌列表
	 */
	public ApiResult<List<BrandWithForestList>> getBrandListWithForests(ItemBrandDO brandDO, boolean needForests) {
		ApiResult<List<BrandWithForestList>> result = new ApiResult<List<BrandWithForestList>>();
		List<BrandWithForestList> brandListWithForestList = Lists.<BrandWithForestList>newArrayList();
		try {
			// 查询符合条件的品牌列表
			List<ItemBrandDO> itemBrandList = (List<ItemBrandDO>) this.find(brandDO);
			if (CollectionUtils.isEmpty(itemBrandList)) {
				return ApiResult.newSuccessResult();
			}
			result.setTotalCount(itemBrandDao.findCnt(brandDO));

			for (ItemBrandDO brandTemp : itemBrandList) {
				// 拷贝属性
				BrandWithForestList brandWithForestList = new BrandWithForestList();
				BeanUtils.copyProperties(brandTemp, brandWithForestList);

				// 查询类目和品牌的关联
				if (needForests) {
					List<ForestDO> forestList = baseItemForestBrandManager.getForestListByBrandId(brandTemp.getId());
					brandWithForestList.setForests(forestList);
					brandListWithForestList.add(brandWithForestList);
				}
			}
			result.setData(brandListWithForestList);
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return result;
	}

	/**
	 * @param brandId
	 * @param needForests
	 * @return 带有相关类目的品牌细节
	 */
	public ApiResult<BrandWithForestList> getBrandDetailWithForests(long brandId, boolean needForests) {
		ApiResult<BrandWithForestList> result = new ApiResult<BrandWithForestList>();
		try {
			// 查询符合条件的品牌
			ItemBrandDO brand = this.get(brandId);
			if (null == brand) {
				return ApiResult.newErrorResult(BrandErrorEnum.BRAND_IS_NULL.getErrorCode(),
						BrandErrorEnum.BRAND_IS_NULL.getErrorMessage());
			}

			// 拷贝属性
			BrandWithForestList brandWithForestList = new BrandWithForestList();
			BeanUtils.copyProperties(brand, brandWithForestList);

			// 查询类目和品牌的关联
			if (needForests) {
				List<ForestDO> forestList = baseItemForestBrandManager.getForestListByBrandId(brand.getId());
				brandWithForestList.setForests(forestList);
			}
			result.setData(brandWithForestList);
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return result;
	}

	/**
	 * 冻结品牌
	 * 
	 * @param brandId
	 * @param operateId
	 * @return 冻结的结果
	 */
	@NeedOperateFlow(operateIdArgLocation = 1, operateFlowEnum = OperateFlowEnum.BRAND)
	public ApiResult<Boolean> freezeBrand(long brandId, long operateId) {
		ItemBrandDO brandDO = this.get(brandId);
		if (null == brandDO) {
			return ApiResult.newErrorResult(BrandErrorEnum.IS_NULL.getErrorCode(),
					BrandErrorEnum.IS_NULL.getErrorMessage());
		}
		boolean updateSuccess;
		try {
			brandDO = new ItemBrandDO();
			brandDO.setId(brandId);
			brandDO.setStatus(AzerothConstants.ItemBrand.DELETE);
			updateSuccess = this.update(brandDO) > 0;
		} catch (Exception e) {
			log.error("baseBrandManager.freezeBrand error, operateId=" + operateId, e);
			return ApiResult.newErrorResult(BrandErrorEnum.SYSTEM_ERROR.getErrorCode(),
					BrandErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return updateSuccess ? ApiResult.newSuccessResult(true)
				: ApiResult.newErrorResult(BrandErrorEnum.SYSTEM_ERROR.getErrorCode(),
						BrandErrorEnum.SYSTEM_ERROR.getErrorMessage(), false);
	}

	/**
	 * 解冻品牌
	 * 
	 * @param brandId
	 * @param operateId
	 * @return 解冻的结果
	 */
	@NeedOperateFlow(operateIdArgLocation = 1, operateFlowEnum = OperateFlowEnum.FOREST)
	public ApiResult<Boolean> resumeBrand(long brandId, long operateId) {
		ItemBrandDO brandDO = this.get(brandId);
		if (null == brandDO) {
			return ApiResult.newErrorResult(BrandErrorEnum.IS_NULL.getErrorCode(),
					BrandErrorEnum.IS_NULL.getErrorMessage());
		}
		boolean updateSuccess;
		try {
			brandDO = new ItemBrandDO();
			brandDO.setId(brandId);
			brandDO.setStatus(AzerothConstants.ItemBrand.NORMAL);
			updateSuccess = this.update(brandDO) > 0;
		} catch (Exception e) {
			log.error("baseBrandManager.resumeBrand error, operateId=" + operateId, e);
			return ApiResult.newErrorResult(BrandErrorEnum.SYSTEM_ERROR.getErrorCode(),
					BrandErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return updateSuccess ? ApiResult.newSuccessResult(true)
				: ApiResult.newErrorResult(BrandErrorEnum.SYSTEM_ERROR.getErrorCode(),
						BrandErrorEnum.SYSTEM_ERROR.getErrorMessage(), false);
	}
	
	
	/**
	 * 按照品牌名精确查找品牌类
	 * @param title
	 * @return 品牌名为title的品牌类
	 */
	private ItemBrandDO findByTitle(String title) {
		return itemBrandDao.findByTitle(title);
	}

}
